﻿//*************************************************************************
//面向服务的协作式智能交通系统仿真软件SoC-ITSS v2.1 alpha
//发布单位：西北工业大学--巴黎高科MINES机器人与群智能系统联合实验室
//团队负责人：张凯龙 Zhang Kailong
//团队成员：张大方、杨安胜、王敏、谢策、王雨佳、谢尘玉等
//联系我们：kl.zhang@nwpu.edu.cn
//2012-2021
//*************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CyberCarSimulation.Bean;
using CyberCarSimulation.Regulation;
using CyberCarSimulation.RoadModel;
using CyberCarSimulation.Cloud;

//*******************************************************
//
//文件名(File Name):           MainData.cs
//
//数据表(Tables):              nothing 
//
//          修改理由：         增加属性、方法
//
//*******************************************************
namespace CyberCarSimulation.Bean
{

    //整个软件的全局变量
    class MainData
    {


        public static Object __ITSLockWasTaken = new object();  //全局鎖

        public static SimulationConfig SimulationConfig;          //全局的软件信息

        public static List<IntersectionPolicyConfig> IntersectionAlgorithmList = new List<IntersectionPolicyConfig>(); //存储不同路口不同算法

        //public static List<string> Algorithm = new List<string>();        

        public static FormConfig FormConfig;                      //现实界面信息

        public static Map Map;                                    //全局的地图信息

        public static TrafficCloudBPR TrafficCloud;                  //交通云

        public static List<Way> TotalWayList = new List<Way>();   //全局道路集合
        public static List<Way> CenterWayList = new List<Way>();  //中间道路集合

        public static List<Lane> TotalLaneList;                   //所有的车道集合
        public static List<Lane> EnterLaneList;                   //进入车道的ID集合
        public static List<Lane> LeaveLaneList;                   //离开车道的ID集合 
        public static List<Lane> CenterLaneList;                  //中间车道的ID集合

        public static List<Intersection> TotalIntersectionList;   //所有的节点路口集合
        public static List<Intersection> CEIntersectionList;      //中间节点的路口集合
        public static List<Intersection> SEIntersectionList;      //进出节点的路口集合

        public static List<Critical> TotalCriticalList;           //所有临界区的集合



       
        //更新车道Lane的List   ？？？不知道，update之后顺序会变，对软件是否有影响
        public static void UpdateLaneList(Lane LaneTemp)
        {
            string LaneID = LaneTemp.GetID();
            int Number = -1;                   //标记位置


            //更新TotalIntersectionList队列
            for (int i = 0; i < MainData.TotalLaneList.Count; i++)
            {
                if (MainData.TotalLaneList[i].GetID().Equals(LaneID))
                {
                    Number = i;
                }
            }
            if (Number != -1)
            {
                MainData.TotalLaneList[Number] = LaneTemp;
                //MainData.TotalLaneList.RemoveAt(Number);
                //MainData.TotalLaneList.Add(LaneTemp);
            }
            
            //更新EnterLaneList队列
            Number = -1; 
            for (int i = 0; i < MainData.EnterLaneList.Count; i++)
            {
                if (MainData.EnterLaneList[i].GetID().Equals(LaneID))
                {
                    Number = i;
                }
            }
            if (Number != -1)
            {
                MainData.EnterLaneList[Number] = LaneTemp;
            }

            //更新LeaveLaneList队列
            Number = -1; 
            for (int i = 0; i < MainData.LeaveLaneList.Count; i++)
            {
                if (MainData.LeaveLaneList[i].GetID().Equals(LaneID))
                {
                    Number = i;
                }
            }
            if (Number != -1)
            {
                MainData.LeaveLaneList[Number] = LaneTemp;
            }

            //更新CenterLaneList队列
            Number = -1;
            for (int i = 0; i < MainData.CenterLaneList.Count; i++)
            {
                if (MainData.CenterLaneList[i].GetID().Equals(LaneID))
                {
                    Number = i;
                }
            }
            if (Number != -1)
            {
                MainData.CenterLaneList[Number] = LaneTemp;
            }

        }



        //更新路口Intersection的List  
        public static void UpdateIntersectionList(Intersection IntersectionTemp) 
        {            
            string IntersectionID = IntersectionTemp.GetID();
            int Number = -1;    //标记位置
            //更新TotalIntersectionList队列
            for(int i=0; i<MainData.TotalIntersectionList.Count; i++) 
            {
                if (MainData.TotalIntersectionList[i].GetID().Equals(IntersectionID)) 
                {
                    Number = i;
                }
            }
            if (Number != -1)
            {
                MainData.TotalIntersectionList[Number] = IntersectionTemp;
            }

            //更新CEIntersectionList队列
            for (int i = 0; i < MainData.CEIntersectionList.Count; i++)
            {
                if (MainData.CEIntersectionList[i].GetID().Equals(IntersectionID))
                {
                    Number = i;
                }
            }
            if (Number != -1)
            {
                MainData.CEIntersectionList[Number] = IntersectionTemp;
            }

            //更新SEIntersectionList队列
            for (int i = 0; i < MainData.SEIntersectionList.Count; i++)
            {
                if (MainData.SEIntersectionList[i].GetID().Equals(IntersectionID))
                {
                    Number = i;
                }
            }
            if (Number != -1)
            {
                MainData.SEIntersectionList[Number] = IntersectionTemp;
            }            
        }


        //根据路口ID拿到Num
        public static string FindIntersectionByNum(int Num)
        {
            string resultid = "";
            for (int i = 0; i < MainData.CEIntersectionList.Count; i++) 
            {
                Intersection intersection = CEIntersectionList[i];
                string id = intersection.GetID();
                int intersectionnum = int.Parse(id.Substring(3, 2));
                if (intersectionnum == Num) 
                {
                    resultid = id;
                }            
            }
            return resultid;            
        }

        //根据ID获取Intersection对象
        public static Intersection FindIntersectionByLaneID(string id) 
        {
            Intersection IntersectionTemp = null;
            for (int i = 0; i < MainData.CenterLaneList.Count; i++) 
            {
                Intersection intersection = CEIntersectionList[i];
                if (intersection.GetID().Equals(id)) 
                {
                    IntersectionTemp = intersection;
                }
            }
            return IntersectionTemp;
        }

        //根据ID获取Way
        public static Way FindWayByID(string WayID) 
        {
            Way Way = null;
            for (int i = 0; i < MainData.CenterWayList.Count; i++)
            {
                Way way = CenterWayList[i];
                if (way.GetID().Equals(WayID))
                {
                    Way = way;
                }
            }
            return Way;
        }

        //根据ID获取Lane
        public static Lane FindCELaneByID(string LaneID) 
        {
            Lane Lane = null;
            for (int i = 0; i < MainData.CenterLaneList.Count; i++) 
            {
                Lane lane = CenterLaneList[i];
                if (lane.GetID().Equals(LaneID)) 
                {
                    Lane = lane;
                }
            }
            return Lane;
        }


        //根据ID获取Intersection
        public static Intersection FindIntersectionByID(string IntersectionID)
        {
            Intersection Intersection = null;
            for (int i = 0; i < MainData.CEIntersectionList.Count; i++)
            {
                Intersection intersection = CEIntersectionList[i];
                if (intersection.GetID().Equals(IntersectionID))
                {
                    Intersection = intersection;
                }
            }
            return Intersection;
        }


        //根据Intersection的enterid、leaveid获取进入这个路口的车道  
        public static string FindLaneByInterID(string startid, string endid)
        {
            Lane lanetemp = null;
            for (int i = 0; i < MainData.CenterLaneList.Count; i++)
            {
                try
                {
                    Lane lane = CenterLaneList[i];
                    if (lane.GetEnterCEIntID().Equals(endid) && lane.GetLeaveCEIntID().Equals(startid))
                    {
                        lanetemp = lane;
                    }

                }
                catch (System.Exception e)
                {
                }
            }

            string LaneStr = "";            
            LaneStr = lanetemp.GetID();            
            return LaneStr;
        }



    }
}
